local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHScreenBase());
local CommonOHOH =_G.CommonOHOH
local CallBackManager = ClientOHOH.load.CallBackManager();
local BtnHelper = ClientOHOH.load.BtnHelper();
local LanguageHelper=_G.LanguageHelper 
local Asset_Cfg=ClientOHOH.config.Asset
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local ConnectClass=_G.CommonOHOH.load.Connect();


function InitInsts(ui)
	ui.insts = {};
	ui.insts.CostIcon ="Frame.PetsFrame.Cost.CurrencyImage"
	ui.insts.CostCount ="Frame.PetsFrame.Cost.TextLabel"

	ui.insts.list ="Frame.PetsFrame.Frame"


	ui.insts.E ="Frame.btn.E"
	ui.insts.Ed ="Frame.btn.E.con"

	ui.insts.R ="Frame.btn.R"
	ui.insts.Rd ="Frame.btn.R.con"

	ui.insts.T ="Frame.btn.T"
	ui.insts.Td ="Frame.btn.T.con"

    ui.insts.Y ="Frame.btn.Y"
	ui.insts.Yd ="Frame.btn.Y.con"

	ui.insts.Lucky1 ={
		button="Frame.Auto.btn1",
		Name="Frame.Auto.btn1.Percentage",
		State="Frame.Auto.btn1.Equip",
        luckDes="Frame.Auto.btn1.text",
	}

	ui.insts.Lucky2 ={
		button="Frame.Auto.btn2",
		Name="Frame.Auto.btn2.Percentage",
		State="Frame.Auto.btn2.Equip",
        luckDes="Frame.Auto.btn2.text",
	}

	ui.insts.Lucky3 ={
		button="Frame.Auto.btn3",
		Name="Frame.Auto.btn3.Percentage",
		State="Frame.Auto.btn3.Equip",
        luckDes="Frame.Auto.btn3.text",

	}

	ui.insts.AutoDeleteTip="Frame.AutoDelete.TextLabel"
end



function M.new(...)
	return M(...);
end;
function M.ctor(self)
	InitInsts(self)

	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.EggLuckGui"));

    self.HatchEvent=ConnectClass.new()
    self.LuckyBuyEvent=ConnectClass.new()


    CallBackManager:AddRbxConnect(self,self.insts.Lucky1.button.Activated, function()
            if not self.LuckyData["Lucky1"].IsHad then
                self.LuckyBuyEvent:Action(self.LuckyData["Lucky1"])
            end
	end);

    CallBackManager:AddRbxConnect(self,self.insts.Lucky2.button.Activated, function()
        if not self.LuckyData["Lucky2"].IsHad then
            self.LuckyBuyEvent:Action(self.LuckyData["Lucky2"])
        end
	end);

    CallBackManager:AddRbxConnect(self,self.insts.Lucky3.button.Activated, function()
        if not self.LuckyData["Lucky3"].IsHad then
            self.LuckyBuyEvent:Action(self.LuckyData["Lucky3"])
        end
	end);

    CallBackManager:AddRbxConnect(self,self.insts.E.Activated, function()
        self:Hatch("E")
	end);
    CallBackManager:AddRbxConnect(self,self.insts.R.Activated, function()
        self:Hatch("R")
	end);
    CallBackManager:AddRbxConnect(self,self.insts.T.Activated, function()
        self:Hatch("T")
	end);

    CallBackManager:AddRbxConnect(self,self.insts.Y.Activated, function()
        self:Hatch("Y")
	end);


    self:AddProcess(function()
		for _, v in pairs(self.main_inst:GetDescendants()) do
			if v:IsA("GuiButton") then
				BtnHelper:InitButton(v, nil, nil, nil, function() end);
			end;
		end;
	end);
end;


function M.dtor(self)
	M.super.dtor(self);
end;

local ContextActionService = game:GetService("ContextActionService")

function M.Hatch(self,id)

    self.HatchEvent:Action(id)

end

function M.SetLuckData(self,data)
    self.LuckyData=data
    for k,v in pairs(self.LuckyData) do
        self.insts[k].Name.Text=LanguageHelper:GetText(v.name)
        self.insts[k].luckDes.Text=LanguageHelper:GetText(793,{"%%ARG1"},{v.lucky})
        if v.IsHad then
            self.insts[k].State.Visible=true
        else
            self.insts[k].State.Visible=false
        end
    end
end

function M.GetCellParent(self)
    return self.insts.list
end


function M.OnShow(self,att,costIcon,price)
    -- assert(cfg,"cfg==null")



    self:BindKeyCode()

    -- self.EggCfg=cfg

    -- if cfg.isR then --R币购买
    --     self.insts.Ed.Text="x "..cfg.info[1][2]
    --     self.insts.Rd.Text="x "..cfg.info[2][2]

    --     self.insts.T.Visible=false
    --     self.insts.Td.Text="x "..cfg.info[3][2]
    -- else
    --     self.insts.T.Visible=true
    --     self.insts.Ed.Text=LanguageHelper:GetText(57) 
    --     self.insts.Rd.Text=LanguageHelper:GetText(58) 
    --     self.insts.Td.Text=LanguageHelper:GetText(59) 
    --     self.insts.Yd.Text=LanguageHelper:GetText(599) 

    -- end

    self.main_inst.Adornee=att


    self.insts.CostIcon.Image =costIcon
    self.insts.CostCount.Text =price --LanguageHelper:GetText(70)

end;



function M.GetDeleteData(self)
    local data={}
    for k,v in pairs(self.DeleteData) do
        if v then
            table.insert(data,k)
        end
    end
    return data
end

function M.BindKeyCode(self)

    ContextActionService:BindAction("HATCH_E", function(actionName, inputState, _inputObject)
        if  actionName == "HATCH_E" and inputState==Enum.UserInputState.End then
            self:Hatch("E")
        end
       
    end, false, Enum.KeyCode.E)

    ContextActionService:BindAction("HATCH_R", function(actionName, inputState, _inputObject)
        if  actionName == "HATCH_R" and inputState==Enum.UserInputState.End then
            self:Hatch("R")
        end
       
    end, false, Enum.KeyCode.R)

    ContextActionService:BindAction("HATCH_Y", function(actionName, inputState, _inputObject)
        if  actionName == "HATCH_Y" and inputState==Enum.UserInputState.End then
            self:Hatch("T")
        end
       
    end, false, Enum.KeyCode.T)

    
    ContextActionService:BindAction("HATCH_T", function(actionName, inputState, _inputObject)
        if  actionName == "HATCH_T" and inputState==Enum.UserInputState.End then
            self:Hatch("T")
        end
       
    end, false, Enum.KeyCode.T)

end

function M.CloseKeyCode(self)
    ContextActionService:UnbindAction("HATCH_E")
    ContextActionService:UnbindAction("HATCH_R")
    ContextActionService:UnbindAction("HATCH_T")
    ContextActionService:UnbindAction("HATCH_Y")
end

function M.OnHide(self)
    self:CloseKeyCode()
    self.main_inst.Adornee=nil
    self.hatchId=nil
end;


return M
